package com.zzzgroup;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import android.os.WorkSource;

public final class PomodoroTime {
	// time in seconds
	private long _workTime, _restTime;

	private TimeUnit _unit;

	public PomodoroTime(long workTime, long restTime) {
		_workTime = workTime;
		_restTime = restTime;
		_unit = TimeUnit.SECONDS;

	}

	public PomodoroTime(long workTime, long restTime, TimeUnit unit) {
		_unit = unit;
		_workTime = getTimeInSeconds(workTime);
		_restTime = getTimeInSeconds(restTime);
		_unit = TimeUnit.SECONDS;
	}

	public String getWorkTimeText() {
		return getTimeInString(_workTime);
	}

	public String getRestTimeText() {
		return getTimeInString(_restTime);
	}

	public long getWorkTime() {
		return _workTime;
	}

	public long getRestTime() {
		return _restTime;
	}

	public int getWorkTimeInMinutes() {
		return getTimeInMinutes(_workTime);
	}

	public int getRestTimeInMinutes() {
		return getTimeInMinutes(_restTime);
	}

	private int getTimeInMinutes(long time) {
		switch (_unit) {
		case MICROSECONDS:
			return (int) time / 1000000 / 60;
		case MILLISECONDS:
			return (int) time / 1000 / 60;
		case SECONDS:
			return (int) time / 60;
		case MINUTES:
			return (int) time;
		case HOURS:
			return (int) time * 60;
		case DAYS:
			return (int) time * 60 * 24;
		default:
			return (int) time;
		}
	}

	private long getTimeInMS(long time) {
		switch (_unit) {
		case MICROSECONDS:
			return time / 1000;
		case MILLISECONDS:
			return time;
		case SECONDS:
			return time * 1000;
		case MINUTES:
			return time * 1000 * 60;
		case HOURS:
			return time * 1000 * 60 * 60;
		case DAYS:
			return time * 1000 * 60 * 60 * 24;
		default:
			return time;
		}
	}

	private long getTimeInSeconds(long time) {
		switch (_unit) {
		case MICROSECONDS:
			return time / 1000000;
		case MILLISECONDS:
			return time / 1000;
		case SECONDS:
			return time;
		case MINUTES:
			return time * 60;
		case HOURS:
			return time * 60 * 60;
		case DAYS:
			return time * 60 * 60 * 24;
		default:
			return time;
		}
	}

	public void DecrementWorkTimeBySecond() {
		_workTime--;
	}

	public void DecrementRestTimeBySecond() {
		_restTime--;
	}

	public void SetWorkTime(long value) {
		_workTime = value;
	}

	public void SetRestTime(long value) {
		_restTime = value;
	}

	private String getTimeInString(long timeInSeconds) {
		int seconds = (int) (timeInSeconds);
		int minutes = seconds / 60;
		seconds = seconds % 60;
		String leadingZero = (seconds < 10) ? ":0" : ":";
		return "" + minutes + leadingZero + seconds;
	}
}
